import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
import { Repository } from 'typeorm';
import { User } from './user/entities/user.entity';
import { Role } from './role/entities/role.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Permission } from './permission/entities/permission.entity';
import { UserService } from './user/user.service';
import { PermissionService } from './permission/permission.service';
import { RoleService } from './role/role.service';
import { AppException } from './exceptions/AppException';
import { HttpStatusCodeEnum } from './enums/http-status-code.enum';

@Injectable()
export class AppService implements OnApplicationBootstrap {
  private readonly logger = new Logger(AppService.name);

  constructor(
    private readonly userService: UserService,
    private readonly roleService: RoleService,
    private readonly permissionService: PermissionService,
  ) {}

  getHello() {
    return {
      name: 'zs',
    };
  }

  async onApplicationBootstrap() {
    try {
      // await this.permissionService.importPermissions();
      console.log('初始化权限表成功...');
    } catch (e) {
      this.logger.error(e.message);
      throw new AppException(HttpStatusCodeEnum.DATABASE_ERROR);
    }

    try {
      // await this.roleService.importRoles();
      console.log('初始化角色表成功...');
    } catch {
      throw new AppException(HttpStatusCodeEnum.DATABASE_ERROR);
    }
  }
}
